from leonardo.cms.models import StaticFile, Page, Category, Comment
from django.contrib.auth.models import User

from datetime import datetime

jtauber = User.objects.get(pk=1)

print "CONVERTING"

import os
import cPickle as pickle
import shutil
import re

os.chdir('cms')
for dirname, dirs, files in os.walk('lfs'):
    print dirname
    if dirname == 'lfs':
        continue
    if not dirname.startswith('lfs/'):
        assert False, dirname
    if not dirname.endswith('.ldv'):
        if dirname.find(".ldv/__") == -1:
            continue
        else:
            steps = dirname.split("/")
            path = "/".join(steps[:-1])[4:-4]
            comment_dir = steps[-1]
            properties = pickle.load(file(os.path.join(dirname, 'property_pickle')))
            last_modified = properties.get('last_modified')
            creation_time = properties.get('creation_time')
            author_name = properties.get('author_name')
            author_link = properties.get('author_link')
            content = file(os.path.join(dirname, '__content__.txt')).read()
            page = Page.objects.get(path=path)
            if creation_time:
                creation_time = datetime.fromtimestamp(creation_time)
            if last_modified:
                last_modified = datetime.fromtimestamp(last_modified)
            comment = Comment(
                page = page,
                comment = content,
                last_modified = last_modified,
                creation_time = creation_time,
                author_name = author_name,
                author_link = author_link,
            )
            print "saving comment", author_name, author_link
            comment.save()
    path = dirname[4:-4]
    
    if path.endswith('~'):
        continue
    if path in ['__page_template__']:
        continue
    if path.startswith('draft'):
        continue
    if path.startswith('__draft__'):
        continue
    property_pickle = False
    has_content = False
    for filename in files:
        if filename.endswith('~'):
            continue
        elif filename in ['property_db', 'content.bak']:
            continue
        elif filename == "property_pickle":
            property_pickle = True
            properties = pickle.load(file(os.path.join(dirname, filename)))
            allow_comments = properties.get('allow_comments', 'NO')
            author = properties.get('author')
            categories = properties.get('categories', [])
            if 'creation_time' in properties:
                creation_time = float(properties['creation_time'])
            else:
                creation_time = None
            if 'last_modified' in properties:
                last_modified = float(properties['last_modified'])
            else:
                last_modified = None
            page_title = properties.get('page_title')
            continue
        elif filename.startswith('__content__.'):
            ext = filename[12:]
            if ext in ['wiki04', 'wikiBNL', 'xhtml']:
                if has_content:
                    assert False
                has_content = True
                static = False
                content_type = ext
                content = file(os.path.join(dirname, filename)).read()
                continue
            else:
                if has_content:
                    assert False, dirname
                has_content = True
                static = True
                static_filename = filename
                content_type = ext
                continue
        elif filename == 'creation_time':
            creation_time = float(file(os.path.join(dirname, filename)).read())
        else:
            assert False, filename
    assert property_pickle
    assert has_content
    if creation_time:
        creation_time = datetime.fromtimestamp(creation_time)
    if last_modified:
        last_modified = datetime.fromtimestamp(last_modified)
    if not creation_time:
        if re.match(r'blog/(\d{4})/(\d{2})/(\d{2})/', path):
            y, m, d = [int(x) for x in re.match(r'blog/(\d{4})/(\d{2})/(\d{2})/', path).groups()]
            creation_time = datetime(y, m, d)
        else:
            creation_time = last_modified

    if static:
        if path.find("__com") != -1:
            continue 
        if not os.access(os.path.join("STATIC", os.path.dirname(path)), os.F_OK):
            os.makedirs(os.path.join("STATIC", os.path.dirname(path)))
        shutil.copy(os.path.join(dirname, static_filename), os.path.join("STATIC", path))
        static_file = StaticFile(path=path, content_type=content_type, author=jtauber, last_modified=last_modified, creation_time=creation_time)
        print "saving static file"
        static_file.save()
    else:
        page = Page(
            path = path,
            content = content,
            content_type = content_type,
            page_title = page_title,
            author = jtauber,
            creation_time = creation_time,
            last_modified = last_modified,
            allow_comments = allow_comments,
        )
        print "saving page"
        page.save()
        for category in categories:
            cat, status = Category.objects.get_or_create(term=category)
            page.categories.add(cat)
        print "saving categories on page"
        page.save()
